package com.rs.service.impl.rs;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rs.dao.PerforscMapper;
import com.rs.dao.ProrateMapper;
import com.rs.dao.TaskMapper;
import com.rs.dao.UserMapper;
import com.rs.model.CommonResult;
import com.rs.model.Perforsc;
import com.rs.model.Task;
import com.rs.service.rs.TaskService;
import com.rs.util.TimeTypeConversionUtil;

@Service
public class TaskServiceImpl implements TaskService {

	@Autowired
	private TaskMapper tm;
	@Autowired
	private UserMapper um;
	@Autowired
	private ProrateMapper ptem;
	@Autowired
	private PerforscMapper perm;

	/***********APP**********/
	/**
	 * 员工添加任务绩效考核
	 */
	@Override
	@Transactional
	public CommonResult<String> taskAddApp(List<Task> tList) {
		//System.out.println("月份：" + TimeTypeConversionUtil.getMonthDate());
		for (Task task : tList) {
			//设置当前月份
			task.setTa_month(TimeTypeConversionUtil.getMonthDate());
			task.setTa_type(0);//设置状态
			tm.taskAddApp(task);
		}
		return new CommonResult<String>(200, "添加成功");
	}

	/**
	 * 员工依据月份查询任务绩效
	 */
	@Override
	public CommonResult<List<Task>> taskQueryByMonthApp(Task task) {
		//将日期设为一号
		task.setTa_month(TimeTypeConversionUtil.conversionTime(task.getTa_month()));
		List<Task> list = tm.taskQueryByMonthApp(task);
		return new CommonResult<List<Task>>(200, "查询成功", list);
	}

	/**
	 * 员工修改任务绩效
	 */
	@Override
	@Transactional
	public CommonResult<String> taskUpdateApp(List<Task> tList) {
		for (Task task : tList) {
			tm.taskUpdateApp(task);
		}
		return new CommonResult<String>(200, "修改成功");
	}

	/**
	 * 上级查看所有未评价的员工任务绩效
	 */
	@Override
	public CommonResult<List<Task>> taskSuperiorLookApp(Task task) {
		//获得上级的部门id
		Integer su_deid = um.userLookInfoApp(task.getTa_suid()).getU_deid();
		//获得时间
		Date ta_month = TimeTypeConversionUtil.conversionTime(task.getTa_month());
		//获得type
		Integer ta_type = task.getTa_type();
		List<Task> list = tm.taskSuperiorLookApp(su_deid, ta_month, ta_type);
		return new CommonResult<List<Task>>(200, "查询成功", list);
	}

	/**
	 * 上级评分
	 */
	@Override
	@Transactional
	public CommonResult<String> taskSuperiorAddEvaluateApp(List<Task> tList) {
		double score = 0.0;//上级评价总分
		for (Task task : tList) {
			task.setTa_type(1);//更改为已评价
			tm.taskSuperiorAddEvaluateApp(task);
			score = score + task.getTa_superiors();
		}

		//从数据库中取出任务比例
		double ta_ratio = Double.valueOf(ptem.queryProrate().getPro_ta()) / 100;

		//计算业绩考核分数
		double tasc = score * ta_ratio;//任务绩效的总得分

		//ta_uid
		Integer ta_uid = tList.get(1).getTa_uid();
		//月份
		Date ta_month = tList.get(1).getTa_month();

		//查询数据库是否有该员工的绩效得分
		Perforsc p = perm.queryPerforscExists(ta_uid, ta_month);
		boolean flag = Optional.ofNullable(p).isPresent();
		if (flag) {//数据库有该员工的绩效得分，修改
			//pe_id
			Integer pe_id = p.getPe_id();
			//总得分
			double total = tasc + p.getPe_behsc();
			Perforsc perforsc = new Perforsc(pe_id, tasc, total);
			//修改绩效得分表
			perm.updatePerforsc(perforsc);
		} else {//数据库没有该员工的绩效得分，创建
			//将行为绩效分数设为0
			double total = tasc + 0.0;
			Perforsc perforsc = new Perforsc(ta_uid, tasc, 0.0, total, ta_month);
			//添加绩效得分
			perm.addPerforsc(perforsc);
		}
		return new CommonResult<String>(200, "评分成功");
	}

	/*********************pc*****************/
	/**
	 * pc查询所有的任务绩效
	 */
	@Override
	public CommonResult<Map<String, Object>> taskLookAllPc(Integer pageNum) {
		Page<Task> page = PageHelper.startPage(pageNum, 10);//分页
		List<Task> list = tm.taskLookAllPc();
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());//总条数
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);
	}

	/**
	 * pc条件查询
	 */
	@Override
	public CommonResult<Map<String, Object>> taskConditionsQueryPc(Task task) {
		Page<Task> page = PageHelper.startPage(task.getPageNum(), 10);//分页
		//将日期设为一号
		//task.setTa_month(TimeTypeConversionUtil.conversionTime(task.getTa_month()));
		List<Task> list = tm.taskConditionsQueryPc(task);
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());//总条数
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);
	}

	/**
	 * pc查看任务绩效详情
	 */
	@Override
	public CommonResult<List<Task>> taskDetailsQueryPc(Task task) {
		//将日期设为一号
		//task.setTa_month(TimeTypeConversionUtil.conversionTime(task.getTa_month()));
		List<Task> list = tm.taskDetailsQueryPc(task);

		return new CommonResult<List<Task>>(200, "查询成功", list);
	}
}
